<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 * @version		$Id:$
 */

$this->headTitle($this->TRANSLATOR->translator('menu_edit_page_title') . ' - ' . $this->TRANSLATOR->translator('common_admin_title', 'core'));

$this->headScript($this->APP_STATIC_SERVER . '/js/jquery.editinplace/jquery.editinplace.packed.js')
	 ->headScript($this->APP_STATIC_SERVER . '/js/gio/treetable.js');	 
?>
<div class="content">
	<div class="box header mb0">
		<div class="head"><div></div></div>
		<h2 class="g_a_breadcump">
			<a class="fl" href="<?php echo $this->url('core_dashboard_index'); ?>"><?php echo $this->TRANSLATOR->translator('dashboard_breadcump', 'core'); ?></a>
			<small class="icon arrow_right" style="display: block;"></small>
			<a class="fl" href="<?php echo $this->url('menu_menu_list'); ?>"><?php echo $this->TRANSLATOR->translator('menu_breadcump'); ?></a>
			<small class="icon arrow_right" style="display: block;"></small>
			<a class="fl" href="javascript: void(0);"><?php echo $this->TRANSLATOR->translator('menu_edit_breadcump'); ?></a>
			<div class="clear"></div>
		</h2>
	</div>
</div>
<div class="content">
	<div class="desc">
		<div class="body">
			<!--  -->
			<form action="<?php echo $this->url('menu_menu_edit', $this->menuData); ?>" method="post" id="menuMenuEdit">
				<h4 class="g_a_h4 fl"><?php echo $this->TRANSLATOR->translator('menu_edit_guide'); ?></h4>
				<div class="fr">
					<?php if ($this->ACL->isAllowed('edit', 'menu', 'menu')) : ?>
					<a class="fl g_a_tip editMenuForm g_a_submit_button mr5" href="javascript: void(0);" title="<?php echo $this->STRING->escape($this->TRANSLATOR->translator('menu_button_save_title')); ?>"></a>
					<?php endif; ?>
					<a class="fl g_a_tip g_a_cancel_button mr5" href="javascript: history.go(-1);" title="<?php echo $this->STRING->escape($this->TRANSLATOR->translator('common_back_button', 'core')); ?>"></a>
					<?php if ($this->ACL->isAllowed('list', 'menu', 'menu')) : ?>
					<a class="fl g_a_tip" href="<?php echo $this->url('menu_menu_list'); ?>" title="<?php echo $this->STRING->escape($this->TRANSLATOR->translator('menu_actions_list_title')); ?>">
						<img src="<?php echo $this->APP_RESOURCE_SERVER; ?>/templates/<?php echo $this->APP_TEMPLATE; ?>/skins/<?php echo $this->APP_SKIN; ?>/images/actions_list_icon.png" style="padding: 0 5px 0 0;" />
					</a>
					<?php endif; ?>
					<?php if ($this->ACL->isAllowed('add', 'menu', 'menu')) : ?>
					<a class="fl g_a_tip" href="<?php echo $this->url('menu_menu_add'); ?>" title="<?php echo $this->STRING->escape($this->TRANSLATOR->translator('menu_actions_add_title')); ?>">
						<img src="<?php echo $this->APP_RESOURCE_SERVER; ?>/templates/<?php echo $this->APP_TEMPLATE; ?>/skins/<?php echo $this->APP_SKIN; ?>/images/actions_add_icon.png" style="padding: 0 5px 0 0;" />
					</a>
					<?php endif; ?>
				</div>
				<div class="g_a_line"></div>
				<p>
					<input type="hidden" id="module_id" value="news" />
					<label for="simple_input"><?php echo $this->TRANSLATOR->translator('menu_language'); ?></label>
					<?php echo Modules_Core_Services_Language::languageSelect(array(
																			'name' => 'menu[language]', 
																			'id' => 'languageSelector', 
																			'selected' => $this->menuData['language'])); ?>
					<?php if (isset($this->errorMessages) && isset($this->errorMessages['language'])) : ?>
					<span class="g_a_error"><?php echo $this->TRANSLATOR->translator($this->errorMessages['language']); ?></span>
					<?php endif; ?>						
					<br>
				</p>
				<p class="small">&nbsp;</p>
				<p>
					<label for="simple_input"><?php echo $this->TRANSLATOR->translator('menu_source_item'); ?></label>
					<div class="g_a_translatable">
						<?php echo Modules_Menu_Services_Menu::translatableMenu(array(
																						'name' => 'menu[source_item]', 
																						'id' => 'menu_source_item',
																						'selected' => (null == $this->sourceMenu) ? null : $this->sourceMenu['menu_id'],
																						'disabled' => null), $this->menuData['language']); ?>
						</div>					
					<br>
				</p>
				<p class="small">&nbsp;</p>
				<p>
					<label for="simple_input"><?php echo $this->TRANSLATOR->translator('menu_name'); ?></label>
					<input title="<?php echo $this->STRING->escape($this->TRANSLATOR->translator('menu_name_description')); ?>" type="text" class="text w_20 g_a_tip_right" name="menu[name]" id="name" value="<?php if (isset($this->menuData['name'])) : ?><?php echo $this->menuData['name']; ?><?php endif; ?>" />
					<?php if (isset($this->errorMessages) && isset($this->errorMessages['name'])) : ?>
					<span class="g_a_error"><?php echo $this->TRANSLATOR->translator($this->errorMessages['name']); ?></span>
					<?php endif; ?>
					<br>
				</p>
				<p class="small">&nbsp;</p>
				<p>
					<label for="simple_input"><?php echo $this->TRANSLATOR->translator('menu_description'); ?></label>
					<textarea style="width: 400px; height: 100px;" name="menu[description]" id="description"><?php if (isset($this->menuData['description'])) : ?><?php echo $this->menuData['description']; ?><?php endif; ?></textarea>
					<?php if (isset($this->errorMessages) && isset($this->errorMessages['description'])) : ?>
					<span class="g_a_error"><?php echo $this->TRANSLATOR->translator($this->errorMessages['description']); ?></span>
					<?php endif; ?>
					<br>
				</p>
				<p class="small">&nbsp;</p>
				<div class="g_a_line"></div>
				<p>
					<label><?php echo $this->TRANSLATOR->translator('menu_item_head'); ?></label>
				</p>
				<p class="small">&nbsp;</p>
				
				<p>
					<label><?php echo $this->TRANSLATOR->translator('menu_item_label'); ?></label>
					<input title="<?php echo $this->STRING->escape($this->TRANSLATOR->translator('menu_item_label_description')); ?>" type="text" class="text w_20 g_a_tip_right" name="itemLabel" id="itemLabel" />
					<br>
				</p>
				<p class="small">&nbsp;</p>
				
				<p>
					<label><?php echo $this->TRANSLATOR->translator('menu_item_url'); ?></label>
					<input style="margin-right: 10px;" title="<?php echo $this->STRING->escape($this->TRANSLATOR->translator('menu_item_url_description')); ?>" type="text" class="text w_20 g_a_tip_right fl" name="itemUrl" id="itemUrl" />
					<a id="addItemButton" class="button green" href="javascript: void(0);"><small class="icon check"></small><span><?php echo $this->TRANSLATOR->translator('menu_item_add'); ?></span></a>
					<br>
				</p>
				<p class="small">&nbsp;</p>
				
				<div class="clear g_a_bottom">
					<table class="g_a_ui_table g_a_top" id="itemTable">
						<thead>
							<tr>
								<th class="checkbox w_1"></th>
								<th class="w_40"><?php echo $this->TRANSLATOR->translator('menu_item_label'); ?></th>
								<th class="w_40"><?php echo $this->TRANSLATOR->translator('menu_item_url'); ?></th>
							</tr>
						</thead>
						<tbody>
							<tr rel="-1" id="row_id_0">
								<td class="checkbox w_1"></td>
								<td colspan="2"><span><strong><?php echo $this->TRANSLATOR->translator('menu_item_root'); ?></strong></span></td>
							</tr>
							<?php if (isset($this->menuData['items']) && $this->menuData['items']) : ?>
							<?php foreach ($this->menuData['items'] as $index =>$item) : ?>
							<?php $item = (array)$item; ?>
							<tr id="row_id_<?php echo $index + 1; ?>" class="row_parent_<?php echo $item['parent_id']; ?>" rel="<?php echo $item['depth']; ?>" parentid="<?php echo $item['parent_id']; ?>">
								<td class="w_1"></td>
								<td>
									<div class="content row_draggable" style="cursor: move;">
										<span><?php echo str_repeat('----&nbsp;', $item['depth']); ?></span>
										<strong class="editable"><?php echo $item['label']; ?></strong>
									</div>
									<span>
										<a href="javascript: void(0);" class="deleteAction button red" style="display: none;">
											<small class="icon cross"></small><span><?php echo $this->TRANSLATOR->translator('menu_item_delete'); ?></span>
										</a>
									</span>
								</td>
								<td class="editable"><?php echo $item['link']; ?></td>
							</tr>
							<?php endforeach; ?>
							<?php endif; ?>
						</tbody>
					</table>
					<input type="hidden" id="menu_items" name="menu[items]" value="" />
				</div>
				
				<p class="last buttons_demo">
					<?php if ($this->ACL->isAllowed('add', 'menu', 'menu')) : ?>
					<a class="button editMenuForm green" href="javascript: void(0);"><small class="icon check"></small><span><?php echo $this->TRANSLATOR->translator('common_submit_button', 'core'); ?></span></a>
					<?php endif; ?>
					<a class="button red" href="javascript: history.go(-1);"><small class="icon cross"></small><span><?php echo $this->TRANSLATOR->translator('common_back_button', 'core'); ?></span></a>
					<?php if (isset($this->tokenElement)) : ?><?php echo $this->tokenElement; ?><?php endif; ?>
				</p>
			</form>
			<!--  -->
			<div class="clear"></div>
		</div>
	</div>
	<div class="body_footer">
		<div class="bottom_left"><div class="bottom_right"></div></div>
	</div>
</div>
<script type="text/javascript">
/**
 * Handle selecting the link from Link Provider
 */
function selectLink(route, href, title) {
	/**
	 * Remove all - and blank space from the begining of title
	 */
	title = title.replace(/^(\-+)/, '');
	title = title.replace(/^(\s+)/, '');
	
	$('#itemUrl').val(href);
	$('#itemLabel').val(title);

	$('#addItemButton').trigger('click');

	$('#itemUrl').val('');
	$('#itemLabel').val('');
};

$(document).ready(function() {
	<?php if (($string = $this->MESSENGER->printMessages()) != null) : ?>
	Gio.Message.success('<?php echo $string; ?>');
	<?php endif; ?>
	
	var treeTable = new Gio.TreeTable('itemTable', 'row');
	
	/**
	 * Add menu item
	 */
	$('#addItemButton').click(function() {
		var label = $('#itemLabel').val();
		var url   = $('#itemUrl').val();
		if ('' == label || '' == url) {
			return;
		}

		/**
		 * Create new row and append it to items table
		 */
		var tr = $('<tr/>');
		$(tr).attr('id', 'row_id_0')
			 .attr('rel', '0')	
			 .addClass('row_parent_0');

		var deleteLink = $('<a/>').attr('href', 'javascript: void(0);')
							.addClass('deleteAction button red')
							.append('<small class="icon cross"></small>')
							.append('<span><?php echo $this->TRANSLATOR->translator('menu_item_delete'); ?></span>');
		$(deleteLink).hide().click(function() {
			treeTable.removeRow($(tr));
		});

		var firstTd = $('<td/>').addClass('w_1').appendTo($(tr));
		var labelCell = $('<td/>');
		$(labelCell).append($('<div/>')
					.addClass('content row_draggable')
					.css('cursor', 'move')
					.html('<span></span><strong class="editable">' + label + '</strong>'))
					.append($('<span/>')
					.append($(deleteLink)))
					.appendTo($(tr));
		$('<td/>').addClass('editable').html(url).appendTo($(tr));

		$(tr).hover(function() {
			$(this).find('a.deleteAction').show();
		}, function() {
			$(this).find('a.deleteAction').hide();
		});

		/**
		 * Make the label and link editable
		 */
		$(tr).find('.editable').editInPlace({
			callback: function(unused, enteredText) { 
				return enteredText; 
			},
			bg_over: null,
			bg_out: null
		});

		treeTable.addRow($(tr));
	});

	/**
	 * Submit form handler
	 */
	$('.editMenuForm').bind('click', function() {
		var items = new Array();
		var links = treeTable.getTreeData();
		$('#itemTable').find('tbody tr').each(function(i) {
			if (i > 0) {
				items[i - 1] = {
					depth: links[i - 1].depth,
					item_id: links[i - 1].id,
					left_id: links[i - 1].left_id,
					right_id: links[i - 1].right_id,
					parent_id: links[i - 1].parent_id,
					label: $(this).find('.row_draggable:first .editable').html(),
					link: $(this).find('td:last').html()
				};
			}
		});

		$('#menu_items').val($.toJSON(items));

		/**
		 * Submit form
		 */
		$('#menuMenuEdit').submit(); 
	});

	$('.deleteAction').bind('click', function() {
		var tr = $(this).parents('tr');
		treeTable.removeRow($(tr));
	});

	$('#itemTable tr').hover(function() {
		$(this).find('a.deleteAction').show();
	}, function() {
		$(this).find('a.deleteAction').hide();
	});

	/**
	 * Make the label and link editable
	 */
	$('#itemTable').find('.editable').editInPlace({
		callback: function(unused, enteredText) { 
			return enteredText; 
		},
		bg_over: null,
		bg_out: null
	});
});
</script>
